package com.lb.shopguide.ui.fragment.boss;

import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.CacheDiskUtils;
import com.blankj.utilcode.util.CacheUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lb.shopguide.R;
import com.lb.shopguide.base.BaseCommonFragment;
import com.lb.shopguide.config.AppConfigManager;
import com.lb.shopguide.config.UserConfigManager;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.ui.activity.GuideActivity;
import com.lb.shopguide.ui.view.ClearTitleBar;
import com.lb.shopguide.util.lb.LogoutHelper;
import com.lb.shopguide.util.pic.GlideCleanUtil;

import java.util.HashSet;
import java.util.Set;

import butterknife.BindView;
import butterknife.OnClick;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * Created by kang on 2017/12/26.
 */

public class FragmentBossSettings extends BaseCommonFragment {
    @BindView(R.id.ctb)
    ClearTitleBar ctb;
    @BindView(R.id.layout_setting_push)
    RelativeLayout layoutPush;
    @BindView(R.id.layout_clear_cache)
    RelativeLayout layoutClearCache;
    @BindView(R.id.switch_push)
    SwitchCompat switchPush;
    @BindView(R.id.tv_logout)
    TextView tvLogout;
    @BindView(R.id.tv_version)
    TextView tvVersion;

    private AppConfigManager mAppConfigManager;


    public static FragmentBossSettings newInstance() {
        return new FragmentBossSettings();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_boss_settings;
    }

    @Override
    public void initData() {
        mAppConfigManager = AppConfigManager.getAppconfigManager(mContext);
        mUserConfigManager = UserConfigManager.getInstance(mContext);
    }

    @Override
    public void initView() {
        ctb.setTvTitle("设置");
        ctb.setLeftImageSrc(R.drawable.iv_back);
        ctb.setOnLeftImageListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                pop();
            }
        });

        bindPushToggle();
        tvVersion.setText("当前版本 V" + AppUtils.getAppVersionName());
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public void sessionOutOfDate() {

    }

    @Override
    public void requestError(String error) {

    }

    private void bindPushToggle() {
        // TODO: 2018/1/31 根据推送设置，设置推送
        switchPush.setChecked(mAppConfigManager.isPushToggle());
    }


    private void logoutSuccess() {
        // TODO: 2018/3/8 清理noti
        clearNotification();
        mUserConfigManager.setUserId("");
        mUserConfigManager.setUserToken("");
        mUserConfigManager.setAccId("");
        mUserConfigManager.setImToken("");
        mUserConfigManager.save2Sp(true);
        JPushInterface.deleteAlias(mContext, AppConstant.SEQUENCE_SET_ALIAS);
        JPushInterface.cleanTags(mContext, AppConstant.SEQUENCE_CLEAN_TAGS);
        getActivity().finish();
        Intent intent = new Intent(mContext, GuideActivity.class);
        intent.putExtra(AppConstant.INTENT_KEY_FROM_LOGOUT, true);
        startActivity(intent);
    }

    private void clearNotification() {
        NotificationManager mNotificationManager = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.cancelAll();
    }

    @Override
    public String getPageTag() {
        return FragmentBossSettings.class.getName();
    }

    @OnClick(R.id.tv_logout)
    void logout() {
        logoutSuccess();
        LogoutHelper.logout();
    }

    @OnClick({R.id.layout_setting_push, R.id.switch_push})
    void setPush() {
        mAppConfigManager.setPushToggle(switchPush.isChecked());
        mAppConfigManager.save2Sp(true);
        if (switchPush.isChecked()) {
            JPushInterface.resumePush(mContext);
        } else {
            JPushInterface.stopPush(mContext);
        }
        bindPushToggle();
    }

    @OnClick(R.id.layout_clear_cache)
    void clearCache() {
        CacheDiskUtils.getInstance().clear();
        GlideCleanUtil.getInstance().clearImageAllCache(mContext);
        ToastUtils.showShort("清除图片缓存成功");
    }
}
